#include "redis_client.h"
#include<exception>
#include"other.h"
#include<iostream>
redis_client::redis_client(const std::string& ip, std::uint16_t port):socket(context)
{
	try {
		socket.connect({ asio::ip::make_address(ip),port });
	}
	catch (const std::exception& ex) {
		std::cerr <<"error: " << ex.what() << std::endl;
		exit(-1);
	}
}

void redis_client::run()
{
	while (true)
	{
		std::string str = cmd.input();
		std::string serialize_str = serialize(str);
		socket.send(asio::buffer(serialize_str));
		std::string res(sizeof(std::uint64_t), '\0');
		socket.receive(asio::buffer(res));
		auto [len, tail] = deserialize_head(res);
		res.resize(len);
		socket.receive(asio::buffer(res));
		cmd.output(res);
	}
}
